import {message} from 'antd';
import {
    queryService,
    queryCategories,
    fetchItem,
    fetchSupport,
    changeSupportItem,
    queryOrderInfo,
    addService,
    changeItem,
    removeService
} from '../services/service_order';

export default {
    namespace: 'service_order',

    state: {
        data: {list: [], pagination: {size: 10, current: 1, total: 0}},
        users: {list: [], pagination: {size: 10, current: 1, total: 0}},
        categories: [],
        info: {},
        supportData: {},
        item: {user: {}, service: {}, location: {}},
        loading: false,
        maskLoading: false,
        confirmLoading: false,
        priceVisible: false,
        chooseUserVisible: false,
        formData: {price: '', support_user: {}},
        submitting: false,
    },

    effects: {
        * fetch({payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryService, payload);
            yield put({
                type: 'save',
                payload: Array.isArray(response.data) ? response : [],
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * info({_}, {call, put}) {
            const response = yield call(queryOrderInfo, {pid: 0});
            yield put({
                type: 'saveInfo',
                payload: response.data,
            });
        },
        * fetchCategories({_}, {call, put}) {
            const response = yield call(queryCategories, {pid: 0});
            const data = response.data;
            const options = data.map((item) => {
                const children = item.child.map((sub) => {
                    sub.value = sub.id;
                    sub.label = sub.name;
                    return sub;
                });
                item.value = item.id;
                item.label = item.name;
                item.children = children;
                return item;
            });

            yield put({
                type: 'saveCategory',
                payload: Array.isArray(options) ? response : [],
            });
        },
        * fetchItem({payload, callback}, {call, put}) {
            yield put({
                type: 'changeMaskLoading',
                payload: true,
            });
            const response = yield call(fetchItem, payload.id);
            if (response.status) {
                const data = response.data;
                yield put({
                    type: 'saveServiceData',
                    payload: data,
                });
                if (callback) {
                    callback(response.data);
                }
            }
            yield put({
                type: 'changeMaskLoading',
                payload: false,
            });
        },
        * edit({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(changeService, payload);
            if (response.status) {
                message.success('编辑成功');
                if (callback) {
                    callback();
                }
            } else {
                message.error(response.message);
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },

        * fecthSupports({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(fetchSupport, payload);
            yield put({
                type: 'saveSupport',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * changeSupport({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(changeSupportItem, payload);
            if (response.status) {
                message.success(response.data);
                if (callback) {
                    callback();
                }
            } else {
                message.error(response.message);
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * change({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(changeItem, payload);
            if (response.status) {
                message.success('修改成功');
                if (callback) {
                    callback();
                }
            } else {
                message.error(response.message);
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * remove({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(removeService, payload);
            if (response.status) {
                message.success('删除成功');
                if (callback) {
                    callback();
                }
            } else {
                message.error(response.message);
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                data: {
                    list: action.payload.data,
                    pagination: {
                        pageSize: action.payload.page.size,
                        total: action.payload.page.total,
                        current: action.payload.page.current
                    }
                },
            };
        },
        saveSupport(state, action) {
            return {
                ...state,
                users: {
                    list: action.payload.data,
                    pagination: {
                        pageSize: action.payload.page.size,
                        total: action.payload.page.total,
                        current: action.payload.page.current
                    }
                },
            };
        },
        * saveStepFormData(state, action) {

            return {
                ...state,
                formData: state.formData,
            };
        },
        saveCategory(state, action) {
            return {
                ...state,
                categories: action.payload.data,
            };
        },
        saveInfo(state, action) {
            return {
                ...state,
                info: action.payload,
            };
        },
        saveServiceData(state, action) {
            return {
                ...state,
                item: action.payload,
            };
        },

        saveSupportChange(state, action) {
            const supportData = state.supportData;
            return {
                ...state,
                supportData: {
                    ...supportData, user_id: action.payload.user_id,
                },
                priceVisible: true,
            }
        }
        ,
        changeLoading(state, action) {
            return {
                ...state,
                loading: action.payload,
            };
        },
        changePriceVisible(state, action) {
            return {
                ...state,
                priceVisible: action.payload,
            };
        },
        changeChooseVisible(state, action) {
            return {
                ...state,
                chooseUserVisible: action.payload,
                supportData: {},
            };
        },
    },
};
